<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Models\Shop\Express;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ExpressController extends Controller
{
   /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
   public function index(Request $request)
   {
      $where = function ($query) use ($request) {
         if ($request->has('keyword') and $request->keyword != '') {
            $cate = $request->cate;
            $search = "%" . $request->keyword . "%";
            $query->where($cate, 'like', $search);
         }
      };

      $expresses = Express::where($where)
         ->orderBy('sort_order', 'desc')
         ->paginate(config('admin.page_size'));

      return response()->json($expresses);
   }


   /**
    * Store a newly created resource in storage.
    *
    * @param \Illuminate\Http\Request $request
    * @return \Illuminate\Http\Response
    */
   public function store(Request $request)
   {
      Express::create($request->all());
   }

   /**
    * Display the specified resource.
    *
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function show($id)
   {
      $express = Express::find($id);

      return response()->json($express);
   }


   /**
    * Update the specified resource in storage.
    *
    * @param \Illuminate\Http\Request $request
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function update(Request $request, $id)
   {
      $express = Express::find($id);
      $express->update($request->all());
   }

   /**
    * Remove the specified resource from storage.
    *
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function destroy($id)
   {
      Express::destroy($id);
   }

   /***
    *  改变属性
    */
   public function change_attr(Request $request)
   {
      $express = Express::find($request->id);
      $express->is_enable = !$request->is_enable;
      $express->save();
   }

   /***
    *  排序
    */
   public function change_sort(Request $request)
   {
      $express = Express::find($request->id);
      $express->sort_order = $request->sort_order;
      $express->save();
   }
}
